package com.ww.juc.completable.future.multi.task;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * allOf、anyOf
 * allOf：CompletableFuture是多个任务都执行完成后才会执行，只有一个任务执行异常，则返回CompletableFuture执行get方法时会抛出异常，如果都是正常执行，则get返回null
 * anyOf：CompletableFuture是多个任务只要一个任务执行完成，则返回CompletableFuture执行get方法时会抛出异常，如果都是正常执行，则get返回执行完成任务的结果
 * @author wanggw
 * 2024年2月5日 下午7:46:13
 */
public class MultiTaskCombineDemo3 {
	
	public static void main(String[] args) {
//		testAllOf();
		
		testAnyOf();
	}

	@SuppressWarnings("unused")
	public static void testAllOf() {
		CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务1干活了...");
			try {
				System.out.println(Thread.currentThread() + "任务1开始睡眠2秒");
				TimeUnit.SECONDS.sleep(2L);
				System.out.println(Thread.currentThread() + "任务1睡眠了2秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread() + "任务1执行完成");
			return "任务1返回了...";
		});
		
		CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务2要干点啥...");
			int a = 1 / 0;
			try {
				System.out.println(Thread.currentThread() + "任务2开始睡眠5秒");
				TimeUnit.SECONDS.sleep(5L);
				System.out.println(Thread.currentThread() + "任务2睡眠了5秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return "任务2干完结束了";
		});
		
		CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务3要干点啥...");
			try {
				System.out.println(Thread.currentThread() + "任务3开始睡眠3秒");
				TimeUnit.SECONDS.sleep(3L);
				System.out.println(Thread.currentThread() + "任务3睡眠了3秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return "任务3干完结束了";
		});
		
		CompletableFuture.allOf(cf1, cf2, cf3);
		System.out.println("所有的都执行完了，stop===============");
		
//		打印结果
//		Thread[ForkJoinPool.commonPool-worker-9,5,main]任务1干活了...
//		Thread[ForkJoinPool.commonPool-worker-9,5,main]任务1开始睡眠2秒
//		Thread[ForkJoinPool.commonPool-worker-2,5,main]任务2要干点啥...
//		Thread[ForkJoinPool.commonPool-worker-11,5,main]任务3要干点啥...
//		Thread[ForkJoinPool.commonPool-worker-11,5,main]任务3开始睡眠3秒
//		所有的都执行完了，stop===============
	}
	
	
	@SuppressWarnings("unused")
	public static void testAnyOf() {
		CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务1干活了...");
			try {
				System.out.println(Thread.currentThread() + "任务1开始睡眠2秒");
				TimeUnit.SECONDS.sleep(2L);
				System.out.println(Thread.currentThread() + "任务1睡眠了2秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread() + "任务1执行完成");
			return "任务1返回了...";
		});
		
		CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务2要干点啥...");
			int a = 1 / 0;
			try {
				System.out.println(Thread.currentThread() + "任务2开始睡眠5秒");
				TimeUnit.SECONDS.sleep(5L);
				System.out.println(Thread.currentThread() + "任务2睡眠了5秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return "任务2干完结束了";
		});
		
		CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务3要干点啥...");
			try {
				System.out.println(Thread.currentThread() + "任务3开始睡眠3秒");
				TimeUnit.SECONDS.sleep(3L);
				System.out.println(Thread.currentThread() + "任务3睡眠了3秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return "任务3干完结束了";
		});
		
		CompletableFuture.anyOf(cf1, cf2, cf3);
		System.out.println("所有的都执行完了，stop===============");
		
//		打印结果
//		Thread[ForkJoinPool.commonPool-worker-9,5,main]任务1干活了...
//		Thread[ForkJoinPool.commonPool-worker-2,5,main]任务2要干点啥...
//		所有的都执行完了，stop===============
//		Thread[ForkJoinPool.commonPool-worker-11,5,main]任务3要干点啥...
//		Thread[ForkJoinPool.commonPool-worker-9,5,main]任务1开始睡眠2秒
//		Thread[ForkJoinPool.commonPool-worker-11,5,main]任务3开始睡眠3秒
	}
}

